<?php

class PhpFox_Mod_Site_ServicePage extends BaseService
{
	function _add($aVar)
	{
		$aData = array();
		$aData['title_head'] = strip_tags($aVar['title_head']);
		$aData['title_menu'] = strip_tags($aVar['title_menu']);
		$aData['title_url'] = $this->_parseUrl($aVar['title_url']);
		$aData['content'] = $aVar['content'];
		$aData['user_id'] = App::getUser('id');
		$aData['meta'] = strip_tags($aVar['meta']);

		$iId = $this->oDatabase->insert(App::getT('pages'), $aData);

		if ( isset($aVar['menu']) && $aVar['menu'] )
		{
			$oSrvMenu = &App::getModuleService('Menu', 'Menu');
			$oSrvMenu->_add(array
			(
				'title_menu' => $aData['title_menu'],
				'location' => $aVar['location'],
				'url' => $aVar['title_url'],
				'parent' => $iId,
			));
		}

		$oCache = &App::newObject("Cache");
   	 	$oCache->rmFile("specialpages.php");

		return $aData['title_url'];
	}

	function _parseUrl($sString)
	{
		$sString = strip_tags($sString);
		$sString = preg_replace( '/ +/', '-',preg_replace('/[^0-9a-zA-Z ]+/', '-', ($sString)));
		return $sString;
	}

	function _deletePage($iId)
	{
		$this->oDatabase->query("DELETE FROM ". App::getT('pages') ." WHERE page_id = '". $iId ."'");

		$oSrvMenu = &App::getModuleService('Menu', 'Menu');
		$oSrvMenu->_delete($iId, "parent");

		return true;
	}

	function _getPages()
	{
		$aPages = $this->oDatabase->getRows("SELECT page_id, title_head, title_url FROM ". App::getT('pages') ." ORDER BY page_id DESC");

		return $aPages;
	}

	function _edit($iId)
	{
		$aRow = $this->oDatabase->getRow("SELECT p.*, m.menu_id AS menu, m.location  FROM ". App::getT('pages') ." AS p
			LEFT JOIN ". App::getT('menu') ." AS m ON(m.parent = '". $iId ."')
			WHERE p.page_id = '". $iId ."'");

		if ( !isset($aRow['page_id']) )
		{
			return;
		}

		return $aRow;
	}

	function _update($aVar, $iId)
	{
		$aData = array();
		$aData['title_head'] = strip_tags($aVar['title_head']);
		$aData['title_menu'] = strip_tags($aVar['title_menu']);
		$aData['title_url'] = $this->_parseUrl($aVar['title_url']);
		$aData['content'] = $aVar['content'];
		$aData['user_id'] = App::getUser('id');
		$aData['meta'] = strip_tags($aVar['meta']);

		$this->oDatabase->update(App::getT('pages'), $aData, " page_id = '". $iId ."' ");

		if ( (isset($aVar['menu']) && $aVar['menu']) || (isset($aVar['menu_id']) && $aVar['menu_id']) )
		{
			$oSrvMenu = &App::getModuleService('Menu', 'Menu');

			if ( isset($aVar['menu_id']) && $aVar['menu_id'] && $aVar['menu'] )
			{
				$oSrvMenu->_update(array
				(
					'title_menu' => $aData['title_menu'],
					'location' => $aVar['location'],
					'url' => $aVar['title_url'],
				), $aVar['menu_id']);
			}
			else
			{
				if ( isset($aVar['menu_id']) )
				{
					$oSrvMenu->_delete($aVar['menu_id'], "menu_id");
				}
				else
				{
					$oSrvMenu->_add(array
					(
						'title_menu' => $aData['title_menu'],
						'location' => $aVar['location'],
						'url' => $aVar['title_url'],
						'parent' => $iId,
					));
				}
			}
		}

		$oCache = &App::newObject("Cache");
   	 	$oCache->rmFile("pages_". str_replace("-", "_", $aVar['old_url']) .".php");
   	 	$oCache->rmFile("specialpages.php");

		return $aData['title_url'];
	}
}

?>